<template>
	<view class="container">
		<view class="addr">
			<view class="addr-line b-b">
				<text class="title">店铺名称</text>
				<input placeholder="请输入店铺名" type="text" class="value" v-model="addressData.shop_name" />
			</view>
			<view class="addr-line b-b">
				<text class="title">所在区域</text>
				<input class="value" v-model="addressData.areaaddress" placeholder="省市区县、乡镇等"   @click="xuanzequyu" />
			</view>
			<view class="addr-picker mask" v-if="addrPicker" @clic.stop="pickerShow">
				<view class="content">
					<view class="title">
						请选择所在地区
						<text class="closure" @click="pickerShow">╳</text>
					</view>
					<view class="choose">
						<view class="choose-item" :class="addrIndex == index ? 'xz' : ''" v-for="(item, index) in choose_item" :key="index" @click="chooseTap(index)">{{ item }}</view>
					</view>
					<scroll-view scroll-y="true" class="addr-menu" scroll-with-animation="true" v-if="addrIndex == 0">
						<view class="addr-item" v-for="item in addr_p" :key="item.Id" @click="addrpTap(item)">{{ item.Name }}</view>
					</scroll-view>
					<scroll-view scroll-y="true" class="addr-menu" scroll-with-animation="true" v-if="addrIndex == 1">
						<view class="addr-item" v-for="item in addr_c" :key="item.Id" @click="addrcTap(item)">{{ item.Name }}</view>
					</scroll-view>
					<scroll-view scroll-y="true" class="addr-menu" scroll-with-animation="true" v-if="addrIndex == 2">
						<view class="addr-item" v-for="item in addr_d" :key="item.Id" @click="addrdTap(item)">{{ item.Name }}</view>
					</scroll-view>
					<scroll-view scroll-y="true" class="addr-menu" scroll-with-animation="true" v-if="addrIndex == 3">
						<view class="addr-item" v-for="item in addr_t" :key="item.Id" @click="addrtTap(item)">{{ item.Name }}</view>
					</scroll-view>
				</view>
			</view>
			<view class="addr-line b-b">
				<text class="title">详细地址</text>
				<input type="text" placeholder="请选择详细地址" class="value-m" v-model="addressData.address"disabled='true' />
				<text class="wlIcon-dizhi wanl-text-red" @tap="chooseLocation"></text>
			</view>
		</view>
		<text class="mix-btn" @click="next">下一步</text>
	</view>
</template>

<script>
	import {
	    mapState 
	} from 'vuex';  
	export default {
		data() {
			return {
				addressData: {
					localshop_id: 0,
					member_id: 0,
					Name: '',
					Mobile: '',
					Addr: '',
					IsDefault: false,
					AreaCode_P: 0,
					AreaCode_C: 0,
					AreaCode_A: 0,
					AreaCode_T: 0,
					Province: '',
					City: '',
					Area: '',
					Township: '',
					addrs: '',
					addr:'',
					Longitude:'',
					Latitude:'',
					areaaddress:'',
					address:''
					
				},
				addrPicker: false,
				name:'',
				addr_p:[],
				addr_c:[],
				addr_d:[],
				addr_t:[],
				index_p: -1,
				index_c: -1,
				index_d: -1,
				pagenum:0,
				type:0,
				addrIndex: 0,
				choose_item: [],
				
				//addressData:[],
			};
		},
		onLoad(option){
			this.pagenum = option.pagenum;
			if(option.type != undefined)
				this.type = option.type;
			this.getadds(1, 0);
			this.getshopinfo();
			
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			xuanzequyu(){
				console.log(this.choose_item);
				this.addrPicker=true
			},
			getshopinfo(){
				this.$api.post({
					url: '/wanlshop/Small/shopinfo',
					data: {
						index:1
					},	success: res => {
						console.log(res)
						this.addressData=res
						
						this.addressData.AreaCode_T=res.areacode_t,
						this.addressData.AreaCode_D=res.quxian_code
						// console.log(this.addressData);
					}
				
				
			})
			},
			
			getArea(level, parentcode) {
				this.$api.post({
					url:'/wanlshop/Small/areas',
					data: {
						type: level,
						code: parentcode
					},
					success: res => {
						// console.log(res,'2222');
						if (level == 1) {
							this.addr_p = res;
							// this.addrIndex = 1;
						}
						if (level == 2) {
							this.addr_c = res;
							this.addrIndex = 1;
						}
						if (level == 3) {
							this.addr_d = res;
							this.addrIndex = 2;
						}
						if (level == 4) {
							this.addr_t = res;
							this.addrIndex = 3;
						}
						}
				})
				},
			//根据code获取在区域中的index位置
			chooseTap(index) {
				if (index == 0) {
					this.addr_menu = this.addr_p;
					this.addrIndex = 0;
				}
				if (index == 1) {
					this.addr_menu = this.addr_c;
					this.addrIndex = 1;
				}
				if (index == 2) {
					this.addr_menu = this.addr_d;
					this.addrIndex = 2;
				}
				if (index == 3) {
					this.addr_menu = this.addr_d;
					this.addrIndex = 3;
				}
			},
			async addrpTap(item) {
				this.addressData.AreaCode_P = item.Code;
				this.addressData.Province = item.Name;
				this.choose_item[0] = item.Name;
				this.choose_item[1] = '请选择';
				this.choose_item[2] = '';
				this.choose_item[3] = '';
				this.getArea(2, this.addressData.AreaCode_P);
				this.addressData.areaaddress = this.addressData.Province 
			},
			async addrcTap(item) {
				this.addressData.AreaCode_C = item.Code;
				this.addressData.City = item.Name;
				this.choose_item[1] = item.Name;
				this.choose_item[2] = '请选择';
				this.choose_item[3] = '';
				this.getArea(3, this.addressData.AreaCode_C);
				this.addressData.areaaddress = this.addressData.Province + this.addressData.City 
			},
			async addrdTap(item) {
				this.addressData.AreaCode_D = item.Code;
				this.addressData.Area = item.Name;
				this.choose_item[2] = item.Name;
				this.choose_item[3] = '请选择';
				this.getArea(4, this.addressData.AreaCode_D);
				this.addressData.areaaddress = this.addressData.Province + this.addressData.City+ this.addressData.Area
			},
			async addrtTap(item) {
				this.addressData.AreaCode_T = item.Code;
				this.addressData.Township = item.Name;
				this.choose_item[3] = item.Name;
				this.addressData.areaaddress = this.addressData.Province + this.addressData.City + this.addressData.Area + this.addressData.Township;
				this.pickerShow();
			},
			pickerShow() {
				this.addrPicker = false;
			},
			switchChange(e) {
				this.addressData.IsDefault = e.detail.value;
			},
			async getadds() {
				this.getArea(1, 0);
			},
			chooseLocation(){
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						this.addressData.Longitude = res.longitude;
						this.addressData.Latitude = res.latitude;
						this.addressData.address = res.name;
						//console.log(this.merchantInfo.Addr)
					}
				})
			}, 
			async bindPickerChange_p(e) {
				
				if(e.target.value == -1){
					this.index_p = 0;
				}else{
					if(this.index_p == e.target.value)
						return;
					else{
						this.index_p = e.target.value;
						console.log(this.merchantInfo)
						this.merchantInfo.Province = this.addr_p[this.index_p].Name
					}
				}
				var [error, res] = await this.getArea(2,this.addr_p[this.index_p].Code);
				if(res.statusCode == 200){
					this.addr_c = res.data;
					this.addr_d = [];
					this.index_c = -1;
					this.index_d = -1;
				}
			},
			async bindPickerChange_c(e) {
				if(e.target.value == -1){
					this.index_c = 0;
				}else{
					if(this.index_c == e.target.value)
						return;
					else{
						this.index_c = e.target.value;
						this.merchantInfo.City = this.addr_c[this.index_c].Name
					}
				}
				var [error, res] = await this.getArea(3,this.addr_c[this.index_c].Code);
				if(res.statusCode == 200){
					this.addr_d = res.data;
					this.index_d = -1;
				}
			},
			async bindPickerChange_d(e) {
				if(e.target.value == -1){
					this.index_d = 0;
				}else{
					this.index_d = e.target.value;
					this.merchantInfo.Area = this.addr_d[this.index_d].Name
				}
			},
			next(){
				let data = this.addressData;
				console.log(data);
				if(data.shop_name== ""){
					this.$wanlshop.msg("给店铺起一个名字吧~");
					return;
				}
				
				if (!data.areaaddress) {
					this.$wanlshop.msg('请选择所在区域');
					return;
				}
				if (!data.address) {
					this.$wanlshop.msg('请填写详细地址');
					return;
				}
				if(data.state==1){
					uni.showModal({
						title:'是否重新提交！',
						content:'店铺已通过审核,重新提交需重新审核，确定提交吗？',
						showCancel:true,
						success: (res) => {
							// console.log(res.confirm)
							if(res.confirm){
								this.$api.post({
									url:'/wanlshop/Small/addshop',
									data,
									success: res => {
										uni.navigateTo({
											url:'/pages/user/merchant/orderstep2?pagenum=' + this.pagenum + '&type=' + this.type
										})
									}
									})
							}else{
								uni.navigateTo({
									url:'/pages/user/merchant/orderstep2?pagenum=' + this.pagenum + '&type=' + this.type
								})
							}
							
						}
					})
				}else{
					this.$api.post({
						url:'/wanlshop/Small/addshop',
						data,
						success: res => {
							uni.navigateTo({
								url:'/pages/user/merchant/orderstep2?pagenum=' + this.pagenum + '&type=' + this.type
							})
						}
						})
				}
				
				
				// this.$api.post({
				// 	url:'/wanlshop/Small/addshop',
				// 	data,
				// 	success: res => {
				// 		uni.navigateTo({
				// 			url:'/pages/user/merchant/orderstep2?pagenum=' + this.pagenum + '&type=' + this.type
				// 		})
				// 	}
				// 	})
			}
		}
	}
</script>

<style lang='scss'>

	page{
		background: $page-color-base;
	}
	
	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
	.addr-picker {
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		.content {
			background-color: #fff;
			border-radius: 20rpx 20rpx 0 0;
			height: 90%;
			padding: 0 30rpx;
			.tit {
				position: relative;
				text-align: center;
				font-size: 36rpx;
				margin: 12rpx 0;
				.closure {
					position: absolute;
					top: -10rpx;
					right: 0rpx;
					color: #959595;
				}
			}
			.choose {
				display: flex;
				margin-bottom: 10rpx;
				.choose-item {
					margin-right: 30rpx;
					font-size: 30rpx;
				}
				.xz {
					color: #ff0000;
				}
			}
			.addr-menu {
				height: 87%;
				.addr-item {
					padding: 10rpx 0;
				}
			}
		}
	}
	
	.addr{
		margin: 20upx 25upx;
		background-color: #ffffff;
		display: flex;
		flex-direction: column;
		
		.addr-line{
			display: flex;
			flex-direction: row;
			position:relative;
			padding: 0 20upx;
			color:#75787d;
			font-size: $font-base;
			align-items: center;
			.title{
				width: 25%;
				height: 100upx;
				line-height: 100upx;
			}
			.value{
				width: 70%;
				height: 100upx;
				font-size: $font-base;
			}
			.value-m{
				width: 60%;
				height: 100upx;
			
				font-size: $font-base;
			}
			&.b-b:after{
				left: 0upx;
			}
		}
	}
	
	.icon-xia{
		font-size: $font-lg;
		color: $font-color-light;
		flex: 1;
		text-align: right;
		line-height: 100upx;
	}
	.map:before{
		content: "\e602";
		color: #d81e06;
		font-size: 75upx;
		flex: 1;
		text-align: right;
		line-height: 100upx;
	}
</style>
